<?php

/*
 * Copyright (C) 2014 vlasta81
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Router
 *
 * @author vlasta81
 */

namespace system;

final class router {

    private $geturl = array();

    public function __construct() {
        $urlarray = explode('/', trim(strtolower(preg_replace('/<[^<]+?(.*)>/i', '', filter_input(INPUT_GET, 'url', FILTER_SANITIZE_URL))), DS));
        if (empty($urlarray[0])) : $urlarray[0] = 'home';
        endif;
        if (empty($urlarray[1])) : $urlarray[1] = 'home';
        endif;
        if (empty($urlarray[2])) : $urlarray[2] = 'home';
        endif;
        if (empty($urlarray[3])) : $urlarray[3] = 'default';
        endif;
        $urlarray[0] = strtolower($urlarray[0]);
        $urlarray[1] = strtolower($urlarray[1]);
        $this->geturl = array_filter($urlarray);
    }

    public function getcontroller() {
        return 'application\\' . $this->geturl[0];
    }

    public function gettemplatefile() {
        return './template/' . $this->geturl[0] . '/' . $this->geturl[1] . '.phtml';
    }

    public function getmodel() {
        return 'application\\' . $this->geturl[0] . '\\' . $this->geturl[1] . 'model';
    }

    public function getapp() {
        return $this->geturl[0];
    }

    public function getappaction() {
        return $this->geturl[1];
    }

    public function getappparams() {
        $tempurl = $this->geturl;
        array_shift($tempurl);
        array_shift($tempurl);
        while (!empty($tempurl)) :
            $tmpparams[strtolower($tempurl[0])] = $tempurl[1];
            array_shift($tempurl);
            array_shift($tempurl);
        endwhile;
        return $tmpparams;
    }

}
